package com.barber.service;



import com.barber.entity.Customer;
import com.barber.mapper.CustomerMapper;
import com.barber.util.MD5;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    Logger logger=Logger.getLogger(AdminServiceImpl.class);
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public boolean inserCustomer(Customer customer) {
        logger.debug(customer);

        boolean b = customerMapper.insert(customer);
        logger.debug(b);
        logger.debug(customer);
        return b;
    }
    //
//	@Override
//	public boolean updateCustomer(Customer customer) {
//
//        String sql="update  customer set  username='"+customer.getUsername()
//        +"',password='"+customer.getPassword()
//        +"',phone='"+customer.getPhone()+"'"
//        +"where userid='" +customer.getUserid()+"'" ;
//
//	}
    @Override
    public boolean updateCustomer(Customer customer) {
        logger.debug(customer);
        boolean b = customerMapper.update(customer);
        logger.debug(b);
        return b;
    }

    @Override
    public Map getCustomers(Map map) {
        logger.debug(map);//  {pageIndex=0, sortField=createtime, sortOrder=desc, pageSize=10, password=22, adminid=1, usertype=0, fpemail=2966123846@qq.com, username=222}

        String username = (String) map.get("key");
        int pageIndex = 0;
        int pageSize = 10;

        try {
            pageIndex = Integer.parseInt((String) map.get("pageIndex"));
        } catch (Exception e) {
            // 处理异常或设置默认值
        }

        try {
            pageSize = Integer.parseInt((String) map.get("pagesize"));
        } catch (Exception e) {
            // 处理异常或设置默认值
        }

        // 计算分页的偏移量
        int offset = pageIndex * pageSize;


        List<Customer> customerList = customerMapper.selectCustomersByName(username, offset, pageSize);
        logger.debug(customerList);
        // 查询总数
        int totalCount = customerMapper.selectCustomerCountByName(username);
        logger.debug(totalCount);
        // 将结果放入HashMap中
        map.put("total", totalCount);
        map.put("data", customerList);

        return map;
    }

    @Override
    public Map login(Map map) {
        logger.debug(map);
        String password=MD5.MD5Encode((String) map.get("password"));
        map.put("password",password);
//        Map result= sqlSessionFactory.openSession().selectOne("com.lx.mapper.SellerMapper.select",map);
        Map result= (Map) customerMapper.getOne(map);
        logger.debug(result);

        return result;
    }

    @Override
    public Map getinfo(Map map) {
        logger.debug(map);

//        Map result= sqlSessionFactory.openSession().selectOne("com.lx.mapper.SellerMapper.select",map);
        Map b= customerMapper.getOne(map);
        logger.debug(b);
        return b;
    }

    @Override
    public Boolean updatepass(Map map) {
        logger.debug(map);

//        Map result= sqlSessionFactory.openSession().selectOne("com.lx.mapper.SellerMapper.select",map);
        boolean b= customerMapper.updatepass(map);
        logger.debug(b);
       return b;
    }

    @Override
    public boolean cregister(Map map) {
        logger.debug(map);
        String password=MD5.MD5Encode((String) map.get("password"));
        map.put("password",password);

        boolean b=customerMapper.insert(map);
        logger.debug(b);
        return b;
    }

    @Override
    public boolean deleteCustomer(Map map) {
        logger.debug(map);
        boolean b = customerMapper.delete(map);
        logger.debug(b);
        return b;
    }

    @Override
    public Map password(Map map) {
        logger.debug(map);//{fpemail=2966123846@qq.com}

//                List l= sqlSessionFactory.openSession().selectList("com.eat.mapper.MenuMapper.select", map);
//        Map pass= (Map) customerMapper.getList(map);
        Map pass= (Map) customerMapper.getList(map);
        map.putAll(pass);
        logger.debug(map);
        return map;
    }


}

